// main.js
document.addEventListener('DOMContentLoaded', function() {
    renderMenu();
    setTimeout(() => {
        setActiveMenu('home');
        hideOtherContents();
    }, 0);
});

// 菜单配置
const menuItems = [
    { id: 'home', text: '首页', icon: '🏠' },
    { id: 'student', text: '学生管理', icon: '👥' },
    { id: 'teacher', text: '教师管理', icon: '👨‍🏫' },
    { id: 'course', text: '课程管理', icon: '📚' },
    { id: 'class', text: '班级管理', icon: '🏫' },
    { id: 'college', text: '学院管理', icon: '🏢' },
    { id: 'major', text: '专业管理', icon: '🎓' },
    { id: 'schedule', text: '课程安排', icon: '📅' }
];

// 隐藏除首页外的所有内容区域
function hideOtherContents() {
    const contentSections = document.querySelectorAll('.content-section');
    contentSections.forEach(section => {
        if (!section.id.includes('home')) {
            section.classList.remove('active');
            section.style.display = 'none';
        }
    });
}

// 显示指定内容区域
function showContent(menuId) {
    document.querySelectorAll('.content-section').forEach(section => {
        section.classList.remove('active');
        section.style.display = 'none';
    });

    const contentElement = document.getElementById(menuId+'-content');
    if (contentElement) {
        contentElement.classList.add('active');
        contentElement.style.display = 'block';
    } else {
        document.getElementById('home-content').classList.add('active');
        document.getElementById('home-content').style.display = 'block';
    }
}

// 渲染菜单
function renderMenu() {
    const menuList = document.getElementById('menuList');
    menuList.innerHTML = '';

    menuItems.forEach(item => {
        const li = document.createElement('li');
        const a = document.createElement('a');
        a.href = '#';
        a.dataset.menu = item.id;
        a.onclick = function(e) {
            e.preventDefault();
            switchContent(item.id);
        };

        const iconSpan = document.createElement('span');
        iconSpan.className = 'menu-icon';
        iconSpan.textContent = item.icon;

        const textSpan = document.createElement('span');
        textSpan.className = 'menu-text';
        textSpan.textContent = item.text;

        a.appendChild(iconSpan);
        a.appendChild(textSpan);
        li.appendChild(a);
        menuList.appendChild(li);
    });
}

// 切换内容显示
function switchContent(menuId) {
    showContent(menuId);
    setActiveMenu(menuId);


    switch (menuId){
        case 'student':
            if (typeof studentAppInstance !== 'undefined' && typeof studentAppInstance.loadStudents === 'function') {
                console.log('加载学生数据...');
                studentAppInstance.loadStudents();
            }
            break;
        case 'teacher':
            if (typeof teacherAppInstance !== 'undefined' && typeof teacherAppInstance.loadTeachers === 'function') {
                console.log('加载教师数据...');
                teacherAppInstance.loadTeachers();
            }
            break;
        case 'college':
            if (typeof collegeAppInstance !== 'undefined' && typeof collegeAppInstance.loadColleges === 'function') {
                console.log('加载学院数据...');
                collegeAppInstance.loadColleges();
            }
            break;
        case 'major':
            if (typeof majorAppInstance !== 'undefined' && typeof majorAppInstance.loadMajors === 'function') {
                console.log('加载专业数据...');
                majorAppInstance.loadMajors();
            }
            break;
        case 'class':
            if (typeof classAppInstance !== 'undefined' && typeof classAppInstance.loadClasses === 'function') {
                console.log('加载班级数据...');
                classAppInstance.loadClasses();
            }
            break;
        case 'course':
            if (typeof courseAppInstance !== 'undefined' && typeof courseAppInstance.loadCourses === 'function') {
                console.log('加载课程数据...');
                courseAppInstance.loadCourses();
            }
            break;
        case 'schedule':
            if (typeof scheduleAppInstance !== 'undefined' && typeof scheduleAppInstance.loadSchedules === 'function') {
                console.log('加载课程安排数据...');
                scheduleAppInstance.loadSchedules();
            }
            break;
    }
}

// 设置激活菜单项
function setActiveMenu(menuId) {
    document.querySelectorAll('.sidebar a').forEach(link => {
        link.classList.remove('active');
    });

    const activeLink = document.querySelector('.sidebar a[data-menu='+menuId+']');
    if (activeLink) {
        activeLink.classList.add('active');
    }
}

// 退出登录
function logout() {
    alert('确定要退出吗')
    window.location.href = window.contextPath+'/api/logout';
}

// 折叠侧边栏功能
function toggleSidebar() {
    const sidebar = document.getElementById('sidebar');
    const toggleBtn = document.querySelector('.toggle-btn i');

    sidebar.classList.toggle('collapsed');

    if (sidebar.classList.contains('collapsed')) {
        toggleBtn.classList.remove('fa-angle-left')
        toggleBtn.classList.add('fa-angle-right');
    } else {
        toggleBtn.classList.remove('fa-angle-right')
        toggleBtn.classList.add('fa-angle-left');
    }
}

// 显示错误提示模态框
function showErrorModal(error) {
    const modal = document.getElementById('errorModal');
    const errorMessage = document.getElementById('errorMessage');
    errorMessage.textContent = error;
    modal.classList.add('show');
    setTimeout(closeErrorModal, 10000)
}

// 关闭错误提示模态框
function closeErrorModal(){
    document.getElementById('errorModal').classList.remove('show');
}
